﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class CompanyGreenNumAddControl : YuanlinUserControl
    {
        protected ValuedTextBox greenNumValuedTextBox;
        protected IButton SaveButton;
        protected IButton BackButton;
        private YuanlinUser user = HttpContext.Current.User as YuanlinUser;

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //控制公司最多添加5个号
                CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
                request.PageIndex = 0;
                request.PageSize = 6;
                request.UserId = user.EntityId.Value;
                request.GreenNum = "";
                EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
                if (companyGreenNums.Count > 4)
                {
                    Globals.ShowMessage(Page, "CompanyGreenNumber5", "您所在的权限组最多创建5个绿商号！");
                    return;
                }
            }
            SaveButton.Click += new EventHandler(SaveButton_Click);
            BackButton.Click += new EventHandler(BackButton_Click);
        }

        public void BackButton_Click(object sender, EventArgs e)
        {
            PageRedirect();
        }

        public void SaveButton_Click(object sender, EventArgs e)
        {
            CompanyGreenNumListRequest request = new CompanyGreenNumListRequest();
            request.GreenNum = this.greenNumValuedTextBox.Value;
            EntityList companyGreenNums = BusinessManager.GetCompanyGreenNumList(RequestHelper.BuildGetCompanyGreenNumListRequest(request));
            if (companyGreenNums.Count > 0)
            {
                Globals.ShowMessage(Page, "CompanyGreenNumberExist", "您想要创建的绿商号已经存在！");
                return;
            }
            //预留号码
            RecommendedUserNumberListRequest request1 = new RecommendedUserNumberListRequest();
            request1.Number = this.greenNumValuedTextBox.Value;
            EntityList recommendedUserNumbers = BusinessManager.GetRecommendedUserNumberList(RequestHelper.BuildGetRecommendedUserNumberListRequest(request1));
            if (recommendedUserNumbers.Count > 0)
            {
                Globals.ShowMessage(this.Page, "ExistRecommendedUserNumber", "您想要创建的绿商号已经存在！");
                return;
            }

            CompanyGreenNum companyGreenNum = new CompanyGreenNum();

            companyGreenNum.UserId.Value = user.EntityId.Value;
            companyGreenNum.GreenNum.Value = this.greenNumValuedTextBox.Value;
            companyGreenNum.CreateDate.Value = DateTime.Now;

            BusinessManager.CreateCompanyGreenNum(companyGreenNum);

            Member newMember = new Member();
            newMember.MemberId = user.MemberId.Value;
            newMember.MemberName = companyGreenNum.GreenNum.Value;
            newMember.Password = MemberManager.GetMemberPassword(MemberManager.GetMembersByMemberId(user.MemberId.Value)[0]);
            MemberManager.Create(newMember, null);

            PageRedirect();
        }
        private void PageRedirect()
        {
            Page.Response.Redirect(SiteUrls.Instance().GetCompanyGreenNumListUrl());
        }
    }
}

